/*
 * Copyright 2005 Sun Microsystems, Inc. ALL RIGHTS RESERVED
 * Use of this software is authorized pursuant to the terms of the
 * license found at http://developers.sun.com/berkeley_license.html
 *
 */

 package systemtray;

 import java.awt.*;
 import java.awt.event.*;
 import java.awt.event.AWTEventListener;
 import java.awt.event.AWTEventListener.*;
 import java.awt.Toolkit;
 import java.awt.Toolkit.*;
 import java.awt.event.FocusEvent;
 import java.awt.event.MouseEvent;

 import javax.swing.event.MouseInputListener;
 import java.util.logging.ConsoleHandler;
 import java.util.logging.Formatter;
 import java.util.logging.Level;
 import java.util.logging.LogRecord;
 import java.util.logging.Logger;
 import java.awt.EventQueue;

 public class SystemTrayTest
 {

     public SystemTrayTest()
     {

         final TrayIcon trayIcon;

         if (SystemTray.isSupported()) {

             SystemTray tray = SystemTray.getSystemTray();
             Image image = Toolkit.getDefaultToolkit().getImage("tray.gif");

             MouseListener mouseListener = new MouseListener() {

                 public void mouseClicked(MouseEvent e) {
                     System.out.println("Tray Icon - Mouse clicked! Button:" + e.getButton());
                 }
                 public void mouseEntered(MouseEvent e) {
                     System.out.println("Tray Icon - Mouse entered! Button:" + e.getButton());
                 }
                 public void mouseExited(MouseEvent e) {
                     System.out.println("Tray Icon - Mouse exited! Button:" + e.getButton());
                 }
                 public void mousePressed(MouseEvent e) {
                     System.out.println("Tray Icon - Mouse pressed! Button:" + e.getButton());
                 }
                 public void mouseReleased(MouseEvent e) {
                     System.out.println("Tray Icon - Mouse released! Button:" + e.getButton());
                 }

             };

             // add mouse motion listener to the tray icon
             MouseMotionListener mouseMotionListener = new MouseMotionListener() {

                 public void mouseDragged(MouseEvent e) {
                     System.out.println("Tray Icon - Mouse dragged! Button:" + e.getButton());
                 }

                 public void mouseMoved(MouseEvent e) {
                     System.out.println("Tray Icon - Mouse moved! Button:" + e.getButton());
                 }
             };

             MouseInputListener mouseMotionLister = new MouseInputListener() {

                     public void mouseClicked(MouseEvent e) {
                         System.out.println("Tray Icon - Mouse clicked! Button:" + e.getButton());
                     }
                     public void mouseEntered(MouseEvent e) {
                         System.out.println("Tray Icon - Mouse entered! Button:" + e.getButton());
                     }
                     public void mouseExited(MouseEvent e) {
                         System.out.println("Tray Icon - Mouse exited! Button:" + e.getButton());
                     }
                     public void mousePressed(MouseEvent e) {
                         System.out.println("Tray Icon - Mouse pressed! Button:" + e.getButton());
                     }
                     public void mouseReleased(MouseEvent e) {
                         System.out.println("Tray Icon - Mouse released! Button:" + e.getButton());
                     }

                     public void mouseDragged(MouseEvent e) {
                         System.out.println("Tray Icon - Mouse dragged! Button:" + e.getButton());
                     }

                     public void mouseMoved(MouseEvent e) {
                         System.out.println("Tray Icon - Mouse moved! Button:" + e.getButton());
                     }
             };

             KeyListener keyListener = new KeyListener() {

                 public void keyPressed(KeyEvent e) {
                     System.out.println("Tray Icon - Mouse clicked! Button:" + e.getKeyCode());
                 }

                 public void keyReleased(KeyEvent e) {
                     System.out.println("Tray Icon - Mouse clicked! Button:" + e.getKeyCode());
                 }

                 public void keyTyped(KeyEvent e) {
                     System.out.println("Tray Icon - Mouse clicked! Button:" + e.getKeyCode());
                 }

             };


             ActionListener exitListener = new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                     System.out.println("Exiting...");
                     System.exit(0);
                 }
             };

             PopupMenu popup = new PopupMenu();
             MenuItem defaultItem = new MenuItem("Exit");
             defaultItem.addActionListener(exitListener);
             popup.add(defaultItem);

             trayIcon = new TrayIcon(image, "Tray Demo", popup);

             ActionListener actionListener = new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                     trayIcon.displayMessage("Action Event",
                         "An Action Event Has Been Peformed!",
                         TrayIcon.MessageType.INFO);
                 }
             };

             trayIcon.setImageAutoSize(true);
             trayIcon.addActionListener(actionListener);
             trayIcon.addMouseListener(mouseListener);
             trayIcon.addMouseMotionListener(mouseMotionListener);

             //    Depending on which Mustang build you have, you may need to uncomment
             //    out the following code to check for an AWTException when you add
             //    an image to the system tray.

                try {
                       tray.add(trayIcon);
                } catch (AWTException e) {
                    System.err.println("TrayIcon could not be added.");
                }

         } else {
             System.err.println("System tray is currently not supported.");
         }
     }

     /**
      * @param args the command line arguments
      */
     public static void main(String[] args)
     {

         AWTEventListener dd = new AWTEventListener() {
             @Override
             public void eventDispatched(AWTEvent e) {
                 System.err.println(e);
                 int a = 0;
             }
         };

         Toolkit.getDefaultToolkit().addAWTEventListener(dd,
            MouseEvent.BUTTON1_DOWN_MASK| MouseEvent.BUTTON2_DOWN_MASK |MouseEvent.BUTTON3_DOWN_MASK  | WindowEvent.PAINT_EVENT_MASK | WindowEvent.MOUSE_EVENT_MASK | WindowEvent.WINDOW_EVENT_MASK);

         SystemTrayTest main = new SystemTrayTest();


     }

     private static void logClass(String name) {
         ConsoleHandler consoleHandler = new ConsoleHandler();
         consoleHandler.setLevel(Level.ALL);
         consoleHandler.setFormatter(new Formatter() {
             @Override
             public String format(LogRecord record) {
                 return name + ": " + record.getMessage() + '\n';
             }
         });
         Logger logger = Logger.getLogger(name);
         logger.setLevel(Level.ALL);
         logger.addHandler(consoleHandler);
     }

 }
